package com.chalk.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.chalk.common.constant.CommonConstants;
import com.chalk.common.util.Result;
import com.chalk.config.base.BaseController;
import com.chalk.config.security.SecurityPasswordUtil;
import com.chalk.config.security.UserDetailsUtil;
import com.chalk.model.*;
import com.chalk.service.*;
import com.chalk.vo.MemberVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 会员表(Member)表控制层
 *
 * @author maxiaoqiang
 * @since 2019-03-22 17:38:23
 */
@Api(value = "会员表接口", tags = "会员表模块")
@RestController
@RequestMapping("/v1/member")
public class MemberController extends BaseController {


    @Autowired
    private MemberService memberService;





    /**
     * 逻辑删除会员表
     *
     * @param id 主键集合
     * @return Result 返回统一结果model
     */
//    @ApiOperation(value = "批量删除会员表", notes = "批量删除会员表", response = Result.class)
//    @DeleteMapping
//    public Result delete(
//            @ApiParam(name = "ids", value = "传数组", required = true)
//            @RequestBody String[] ids) {
//        return Result.success(this.memberService.deleteBatchByPrimaryKey(ids));
//    }


    /**
     * 粉丝列表
     *
     * @param current 当前页数
     * @param size    每页显示条数
     * @param member  查询实体
     * @return Result 返回统一结果model
     */
//    @ApiOperation(value = "粉丝列表", notes = "粉丝列表", response = Result.class)
//    @GetMapping("/getFanList")
//    public Result selectFanByPage(Integer current, Integer size, MemberVo member) {
//        SysUser sysUser = (SysUser) UserDetailsUtil.getUserInfo("sysUserService", "user_name");
//        if (null == sysUser) {
//            return Result.notLogin();
//        }
//        if (StringUtils.isEmpty(sysUser.getSellerId()) || CommonConstants.ROOT_PLATFORM_ID.equals(sysUser.getSellerId())) {
//            member.setSellerId(null);
//        } else {
//            member.setSellerId(sysUser.getSellerId());
//        }
//        return Result.success(this.memberService.selectFanPage(super.getBasePage(current, size), member));
//    }


//    @ApiOperation(value = "增加客户", notes = "增加客户", response = Result.class)
//    @GetMapping("/addMemberList")
//    public int


    /**
     * 后台系统-分销员列表分页
     */
    @ApiOperation(value = "分销员列表分页", notes = "分销员列表分页", response = Result.class)
    @GetMapping("getDistributor")
    public Result getDistributor(@RequestParam Map<String, Object> map) {
        /* 获取操作用户 */
        SysUser sysUser = (SysUser) UserDetailsUtil.getUserInfo("sysUserService", "user_name");
        String page = (String) map.get("page");
        String size = (String) map.get("size");
        if (page == null || "".equals(page)) {
            page = "1";
        }
        if (size == null || "".equals(size)) {
            size = "10";
        }
        EntityWrapper<Member> wrapper = new EntityWrapper<>();
        wrapper.eq("distributor_id", 2);
        wrapper.eq("is_deleted", 0);
        //wrapper.setSqlSelect("");
        List<String> descs = new ArrayList<>();
        descs.add("create_time");
        Page<Member> memberPage = new Page<Member>(Integer.valueOf(page), Integer.valueOf(size));
        memberPage.setDescs(descs);
        if (sysUser.getRoleId().equals("1")) {
            //管理员查看全部

            return Result.success(memberService.selectPage(memberPage, wrapper));
        } else if (sysUser.getRoleId().equals("1308606239679139842")) {

            //区域商看自己添加得
            wrapper.eq("reference_id", sysUser.getMemberId());
            return Result.success(memberService.selectPage(memberPage, wrapper));

        }
        return Result.fail("您无权限查看此数据");


    }


    /**
     * 后台系统-新增/修改分销员
     */
    @ApiOperation(value = " 新增/修改分销员", notes = " 新增/修改分销员", response = Result.class)
    @PostMapping("saveDistributor")
    public Result saveDistributor(@RequestBody Member member) {
        /* 获取操作用户 */
        SysUser sysUser = (SysUser) UserDetailsUtil.getUserInfo("sysUserService", "user_name");
        if (member.getMobilePhone() == null || "".equals(member.getMobilePhone())) {
            return Result.fail("手机号码不能为空！");
        }

        if (member.getMemberId() == null || "".equals(member.getMemberId())) {
            Member m = memberService.selectOne(new EntityWrapper<Member>().eq("mobile_phone", member.getMobilePhone()).eq("is_deleted",0));
            if (m != null) {
                return Result.fail("该手机号码已存在！");
            }
        } else {
            Member m = memberService.selectById(member.getMemberId());
            //修改过手机号码，验证其唯一性
            if (!m.getMobilePhone().equals(member.getMobilePhone())) {
                Member m1 = memberService.selectOne(new EntityWrapper<Member>().eq("mobile_phone", member.getMobilePhone()).eq("is_deleted",0));
                if (m1 != null) {
                    return Result.fail("该手机号码已存在！");
                }
            }
        }
        //新增操作
        member.setPassword(SecurityPasswordUtil.md5Encrypt(member.getPassword()));//加密密码
        member.setReferenceId(sysUser.getMemberId()); //推荐人
        member.setDistributorId("2");  //分销员
        member.setNewShopId("123456");//分销员默认绑定门店id
        if (member.getMemberId() == null || "".equals(member.getMemberId())) {
            member.setCreateTime(new Date());
            Boolean f = memberService.insert(member);
            return Result.success(f);

        } else {
            //修改操作
            member.setUpdateTime(new Date());
            Boolean f = memberService.updateById(member);
            return Result.success(f);

        }


    }

    /**
     * 后台系统-逻辑删除分销员
     *
     * @param memberId
     * @return
     */
    @ApiOperation(value = " 逻辑删除分销员", notes = " 逻辑删除分销员", response = Result.class)
    @GetMapping("deleteDistributor")
    public Result deleteDistributor(String memberId) {
        if (memberId == null || "".equals(memberId)) {
            return Result.fail("分销员id不能为空");
        }
        Member member = new Member();
        member.setMemberId(memberId);
        member.setIsDeleted(1);
        Boolean f = memberService.updateById(member);
        return Result.success(f);
    }


//    /**
//     * 后台系统-门店会员列表分页
//     *
//     * @param map
//     * @return
//     */
//    @GetMapping("getCarMembersToPage")
//    public Result getCarMembersToPage(@RequestParam Map<String, Object> map) {
//        /* 获取操作用户 */
//        SysUser sysUser = (SysUser) UserDetailsUtil.getUserInfo("sysUserService", "user_name");
//        String page = (String) map.get("page");
//        String size = (String) map.get("size");
//        String realName=(String)map.get("realName");
//        if (page == null || "".equals(page)) {
//            page = "1";
//        }
//        if (size == null || "".equals(size)) {
//            size = "10";
//        }
//        //条件构造
//        EntityWrapper<Member> wrapper = new EntityWrapper<>();
//        wrapper.eq("member_type", 1);
//        wrapper.isNull("distributor_id");
//        if(realName!=null &&!("".equals(realName))){
//            wrapper.like("real_name",realName);
//        }
//
//        //排序字段
//        List<String> descs = new ArrayList<>();
//        descs.add("create_time");
//        Page<Member> memberPage = new Page<Member>(Integer.valueOf(page), Integer.valueOf(size));
//        memberPage.setDescs(descs);
//        //管理员
//        if (sysUser.getRoleId().equals("1")) {
//            wrapper.isNotNull("new_shop_id");
//            return Result.success(memberService.selectPage(memberPage,wrapper));
//
//        } else if (sysUser.getRoleId().equals("1308606239679139842")) { //区域商
//            List<String> shopIds=memberService.getShopIdsByQusId(sysUser.getMemberId());
//            if(shopIds.size()==0){
//                return Result.fail("暂无数据");
//            }
//            wrapper.in("new_shop_id",shopIds);
//
//            return Result.success(memberService.selectPage(memberPage,wrapper));
//        } else if (sysUser.getRoleId().equals("1308653321790115842")) { //门店
//            wrapper.eq("new_shop_id",sysUser.getShopId());
//
//            return Result.success(memberService.selectPage(memberPage,wrapper));
//        }
//
//        return Result.fail("无权限显示该数据！");
//    }
//



    /**
     * 后台系统-分销员重置密码
     */
    @GetMapping("resetPasswordForDistributor")
    public Result resetPasswordForDistributor(String memberId){
        if(memberId==null || "".equals(memberId)){
            return Result.fail("分销员ID不能为空");
        }
        return Result.success(memberService.resetPassword(memberId));
    }
}